# @Auther: cty
# @Date: 2019/10/31
# @Description: 数据库更新
# @version: 1.0

import pymysql

# 打开数据库连接
db = pymysql.connect("localhost", "cty", "123456", "TESTDB")

# 使用 cursor() 方法创建一个游标对象 cursor
cursor = db.cursor()

# SQL 更新语句
sql = "UPDATE EMPLOYEE SET AGE = AGE + 1 WHERE SEX = '%c'" % ('M')
try:
   # 执行SQL语句
   cursor.execute(sql)
   # 提交到数据库执行
   db.commit()
except:
   # 发生错误时回滚
   db.rollback()

# SQL 查询语句
sql = "SELECT * FROM EMPLOYEE \
       WHERE SEX = '%c'" % ('M')
try:
   # 执行SQL语句
   cursor.execute(sql)
   # 获取所有记录列表
   results = cursor.fetchall()
   for row in results:
      print("update table :%s" %str(row))
except:
   print ("Error: unable to fetch data")

# 关闭数据库连接
db.close()



'''
# 结果：
update table :('Mac', 'Mohan', 21, 'M', 2000.0)
'''